from invenio.config import CFG_SITE_URL
import os, urllib

def format(bfo, active="no", contributors="no", withRoles="no"):
	
	resa = []
	resc = []
	adict = {"authors" : [], "contributors" : []}
	for a in bfo.fields("720"):
		if a.has_key("a"):
			adict["authors"].append(a["a"])
		elif a.has_key("i"):
			adict["contributors"].append((a["i"], a.get("e" ,"")))
			
	for a in adict["authors"]:
		if active.lower() == "yes":
			a = "<a href='{0}'>{1}</a>".format(os.path.join(CFG_SITE_URL, "search?f=author&p=" + urllib.quote(a)), a)
		resa.append(a)
		
	if contributors.lower() == "yes":
		for c in adict["contributors"]:
			cont = c[0]
			if active.lower() == "yes":
				cont = "<a href='{0}'>{1}</a>".format(os.path.join(CFG_SITE_URL, "search?f=author&p=" + urllib.quote(c[0])), c[0])
			if withRoles:
				if c[1]:
					cont += " ({0})".format(c[1])
					
			resc.append(cont)
			
	return " ; ".join(resa + resc)
